
import Foundation
import Flutter
import TunnelKitManager
import ZZBase
import AppTrackingTransparency
import AdSupport



typealias HCVNFFViewController = FlutterViewController
typealias HCVNFFChannel = FlutterMethodChannel
typealias HCVNFFBinaryMessenger = FlutterBinaryMessenger
typealias HCVNFFMethodCall = FlutterMethodCall
// typealias FlutterResult = FlutterResult

struct HCVNFMethod: Equatable {


    var rawValue: String

    static let prepareVpn = HCVNFMethod(rawValue: "vpnPrepare")
    static let connectVpn = HCVNFMethod(rawValue: "connectVpn")
    static let disConnectVpn = HCVNFMethod(rawValue: "disConnectVpn")

    static let vpnStatus = HCVNFMethod(rawValue: "vpnStatus")

    static let openUrl = HCVNFMethod(rawValue: "openUrl")
    static let fbDeepUrl = HCVNFMethod(rawValue: "fbDeepUrl")

    static let requestTrackingAuthorization = HCVNFMethod(rawValue: "requestTrackingAuthorization")
}


class HCVNChannelManager{

    static var toolChannel: HCVNFFChannel!

    /// 注册通信渠道
    static func registerChannel(window: UIWindow?){
        // new add
        let controller : HCVNFFViewController = window?.rootViewController as! HCVNFFViewController
        let channel = HCVNFFChannel(name: "hvn.ovpn.connect", binaryMessenger: controller as! HCVNFFBinaryMessenger)
        channel.setMethodCallHandler(HCVNChannelManager.call(call:result:))
        toolChannel = channel
    }
    /// 广告标识权限获取
    static func requestTrackingAuthorization(call: HCVNFFMethodCall, result:@escaping FlutterResult){
        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization { status in
                if status == .authorized{
                    ZZLog("AD IDFA is \(ASIdentifierManager.shared().advertisingIdentifier.uuidString)")

                }
//                Settings.shared.isAdvertiserTrackingEnabled = true
                result(true)
            }
        } else {
            if ASIdentifierManager.shared().isAdvertisingTrackingEnabled{
                ZZLog("AD IDFA is \(ASIdentifierManager.shared().advertisingIdentifier.uuidString)")
//                Settings.shared.isAdvertiserTrackingEnabled = true
            }
            result(true)
        }
    }

    /// 处理Flutter传回来的值
    static func call(call: HCVNFFMethodCall, result: @escaping FlutterResult){
        let method = HCVNFMethod(rawValue: call.method)
        switch method {
            case .prepareVpn:
                prepareVpn(call: call,result: result)
            case .connectVpn:
                connectVpn(call: call,result: result)
            case .disConnectVpn:
                disConnectVpn(call: call,result: result)
            case .openUrl:
                openUrl(call: call,result: result)
            case .fbDeepUrl:
                fbDeepUrl(call: call,result: result)
            case .requestTrackingAuthorization:
                requestTrackingAuthorization(call: call, result: result)
            default:
                result(FlutterMethodNotImplemented)
                break
        }
    }

    /// 获取应用启动的目标链接
    static func openUrl(call: HCVNFFMethodCall, result:@escaping FlutterResult){
//        result(HVVNGlobal.openURL?.absoluteString ?? "")
        result("")
    }

    /// 获取Facebook应用启动的目标链接
    static func fbDeepUrl(call: HCVNFFMethodCall, result:@escaping FlutterResult){
//        guard let url = HVVNGlobal.fbDeepURL else {
//            AppLinkUtility.fetchDeferredAppLink { url , error in
//                ZZLog("facebook app link \(url?.absoluteString ?? "null")")
//                HVVNGlobal.fbDeepURL = url
//                result(url?.absoluteString ?? "")
//            }
//            return
//        }
//        result(url.absoluteString)

        result("")
    }



//MARK: - VPN

    /// VPN状态改变了
    static func vpnStatusChanged(_ status: VPNStatus){
        HCVNChannelManager
            .toolChannel
            .invokeMethod(HCVNFMethod.vpnStatus.rawValue, arguments: ["status": status.rawStatusNum])
    }
    /// 连接VPN
    static func connectVpn(call: HCVNFFMethodCall, result: FlutterResult){
        guard let ovpnFilePath = call.arguments as? String else {
            result(false)
            return
        }
        HCVNConnectManager.share.hvn_connect(ovpnFilePath)
    }
    /// VPN报错或者链接初始化失败
    static func vpnError(){
        HCVNChannelManager
            .toolChannel
            .invokeMethod(HCVNFMethod.vpnStatus.rawValue, arguments: ["status": -1])
    }

    static func prepareVpn(call: HCVNFFMethodCall, result: FlutterResult){
        HCVNConnectManager.share.hvn_prepare();
        result(true)
    }


    /// 断开连接VPN
    static func disConnectVpn(call: HCVNFFMethodCall, result: FlutterResult){
        HCVNConnectManager.share.hvn_disconnect()
    }
}
